package com.efuture.mutlithread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @ClassNAME CallableThread
 * @Description Jdk8实现线程的新方式 通过实现Callable接口创建线程
 * @Author zhaoshanshan
 * @Date 2021/9/11 1:14 下午
 * @Version 1.0
 */
public class CallableThread {

    public static void main(String[] args) {
        System.out.println("主线程开始");
        FutureTask task = new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                System.out.println("call开始");
                Thread.sleep(10000);
                System.out.println("call结束");
                return 100;
            }
        });
        Thread t = new Thread(task);
        t.start();
        try {
            Object result = task.get();//此方法会造成当前线程阻塞 需要等子线程运行完 才会拿到值
            System.out.println("子线程返回结果:"+result);

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("主线程结束");
    }
}